/**
 * Copyright 2011 Honeywell Federal Manufacturing & Technologies, LLC. This material was produced under U.S. Government
 * contract DE-ACO4-01AL66850, Honeywell Federal Manufacturing & Technologies, LLC, (FM&T) with the U. S. Department of
 * Energy for the operation of the Kansas City Plant . The U.S. Government has rights to use, reproduce, and distribute
 * this software.  NEITHER THE GOVERNMENT NOR Honeywell Federal Manufacturing & Technologies, LLC, MAKES ANY WARRANTY,
 * EXPRESS OR IMPLIED, OR ASSUMES ANY LIABILITY FOR THE USE OF THIS SOFTWARE.  If software is modified to produce
 * derivative works, such modified software should be clearly marked, so as not to confuse it with the version available
 * from FM&T.
 *
 * Additionally, this program is free software; you can redistribute it and/or modify it under the terms of the GNU
 * General Public License Version 3 published by the Free Software Foundation. Accordingly, this program is distributed in
 * the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this library.
 * If not, see http://www.gnu.org/licenses/.
 */
package org.braintrain.tapestry.view;

import com.javaforge.tapestry.spring.annotations.InjectSpring;
import org.apache.tapestry.IAsset;
import org.apache.tapestry.IRequestCycle;
import org.apache.tapestry.annotations.Asset;
import org.braintrain.domain.*;
import org.braintrain.domain.types.ModuleNumbering;
import org.braintrain.services.DeltaResourceManager;
import org.braintrain.services.MediaService;
import org.braintrain.services.NavigationService;
import org.braintrain.services.util.contentBaseVisitor.ContentBaseVisitorResolvingExtractor;

import java.util.List;

/**
 * Tapesry ViewAll page.  This page displays all the production content,
 * in order, on one long page.  Also included is the linked pages linked to
 * by the linking pages.
 * 
 * @author John Ericksen
 *
 */

public abstract class ViewAll extends BrainTrainSecuredBasePage
{
    @Asset("/images/ajaxWaiting.gif")
    public abstract IAsset getAjaxWaitingImage();
    
    @InjectSpring("mediaService")
    public abstract MediaService getMediaService();
    
    @InjectSpring("navigationService")
    public abstract NavigationService getNavigationService();

    @InjectSpring("resourceManager")
    public abstract DeltaResourceManager getResourceManager();
    
    @Asset("/images/checked.gif")
    public abstract IAsset getCheckedImage();    

    
    public abstract Navigation getNavigation();
    public abstract void setNavigation(Navigation nav);
    
    public abstract Question getQuestion();
    public abstract void setQuestion(Question q);
    
    public abstract Answer getAnswer();
    public abstract void setAnswer(Answer a);
    
    public abstract Module getModule();
    public abstract void setModule(Module m);
    
    public abstract boolean isDeltaNavigation();
    public abstract void setDeltaNavigation(boolean d);
    
    /**
     * Initializes the page
     * @param cycle
     * @param module
     */
    public void activate(IRequestCycle cycle, Module module, boolean delta)
    {
        setModule(module);
        setDeltaNavigation(delta);
        
        cycle.activate(this);
    }
    
    //Current Page    
    public Content getCurrentPage() {
        return new ContentBaseVisitorResolvingExtractor(getNavigation(), isDeltaNavigation()).getContent();
    }
    
    //Current Test
    public TestData getCurrentTest(){
        return new ContentBaseVisitorResolvingExtractor(getNavigation(), isDeltaNavigation()).getTest();
    }
    
    public String getFooter(){
        return getResourceManager().getResourceValue(getNavigation().getModule().getFooter(), isDeltaNavigation());
    }
    
    public String getHeader(){
        return getResourceManager().getResourceValue(getNavigation().getModule().getHeader(), isDeltaNavigation());
    }
    
    //View Content getters
    public String getContent(){
        return getResourceManager().getResourceValue(getCurrentPage().getContent());
    }
    
    /**
     * Returns a list of navigations, the main chain
     * @return
     */
    public List<Navigation> getAllPages()
    {
        List<Navigation> mainChain = null;
        if(isDeltaNavigation())
        {
            mainChain = getNavigationService().getMainDeltaNavigationChain(getModule(), false);
        }
        else
        {
            mainChain = getNavigationService().getMainNavigationChain(getModule());
        }
        
        List<List<Navigation>> linkedChains = getNavigationService().getLinkedChains(getModule(), isDeltaNavigation(), false);
        
        for(List<Navigation> linkedChain: linkedChains)
        {
            mainChain.addAll(linkedChain);
        }
        
        return mainChain;
    }
    
    public boolean getInsertPageNumber()
    {
        return getNavigation().getModule().getNumbering() != ModuleNumbering.None &&
        getNavigation().getModule().isActive() &&
        getNavigation().isMainNavigation() &&
        getNavigation().getNavIndex() != null;
    }
}
